package com.wp.service.impl;

import com.wp.mapper.UserMapper;
import com.wp.pojo.PageResult;
import com.wp.pojo.User;
import com.wp.pojo.UserResult;
import com.wp.pojo.basePojo.SysResult;
import com.wp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取用户列表---------每次都会查询数据库----效率低
     */
    @Override
    public PageResult getUserList(PageResult pageResult) {
        Integer num = pageResult.getPageNum();
        Integer size = pageResult.getPageSize();
        String query = pageResult.getQuery();
        Integer startNum = (num - 1) * size;
        //获取用户总数
        Long total = userMapper.getUserCount(query);
        //分页获取所有用户
        List<UserResult> users = userMapper.getUserList(query, startNum, size);
        pageResult.setTotal(total);
        pageResult.setRows(users);
        return pageResult;
    }

    /**
     * 删除用户
     */
    @Override
    public void deleteUser(UserResult userResult) {
        Integer status = userResult.getStatus();
        String uid = userResult.getUid();
        Integer userStatus = (status == 0) ? 0 : 1;
        userMapper.updateUser(uid, userStatus);
    }

    /**
     * 重置用户密码
     */
    @Override
    public String resetUserPassword(User user) {
        Date date = new Date();
//        User user = new User();
        user.setUpdateTime(date);
//        user.setUid(uid);
        //MD5加密
        String password = user.getPassWord();
        String token = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassWord(token);
        userMapper.resetUserPassword(user);
        return token;
    }

    /**
     *用户禁言
     *  */
    @Override
    public Object updateCommentStatus(User user) {
        userMapper.updateCommentStatus(user);
        return SysResult.success();

    }
}